<template>
  <!-- 退货原因设置 returnReason -->
  <!-- 新增 -->
  <el-card class="box_card_add" v-permission="'addUser'" shadow="never">
    <div class="add_btn">
      <div class="add_icon">
        <el-icon><Tickets /></el-icon><span>数据列表</span>
      </div>
      <el-button @click="addFn">添加</el-button>
    </div>
  </el-card>
  <!-- 表格 -->
  <el-table
    :data="tableList"
    border
    style="width: 100%"
    table-layout="auto"
    @select="selectFn"
    @select-all="selectAllFn"
  >
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="编号" prop="id" align="center" />
    <el-table-column label="原因类型" prop="name" align="center" />
    <el-table-column label="排序" prop="sort" align="center"></el-table-column>
    <el-table-column label="是否可用" prop="memberUsername" align="center">
      <template #default="scope"
        ><el-switch
          v-model="scope.row.status"
          :active-value="1"
          :inactive-value="0"
      /></template>
      <!-- @change="updateStatus(scope.row.id, scope.row.status)" -->
    </el-table-column>
    <el-table-column label="添加时间" align="center">
      <template #default="scope">{{ scope.row.createTime }}</template>
    </el-table-column>
    <el-table-column label="操作" align="center" fixed="right">
      <template #default="{ row }">
        <el-button size="small" @click="editFn(row.id)">编辑</el-button>
        <el-button size="small" @click="deleteFn(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 底部 批量操作 分页器 -->
  <div class="bottom">
    <!-- 批量操作 下拉框 -->
    <div>
      <el-select class="m-2" placeholder="批量操作" v-model="seleOper">
        <el-option
          v-for="item in seleData"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" @click="selectDeleteFn">确定</el-button>
    </div>
    <!-- 分页器 -->
    <el-pagination
      background
      v-model:current-page="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :page-sizes="[5, 10, 15]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <!-- 添加退货原因 对话框 -->
  <el-dialog v-model="dialogFormVisible" title="添加退货原因" width="30%">
    <el-form :model="state.form" size="large" ref="formRef">
      <el-form-item label="原因类型：" label-width="100">
        <el-input v-model="state.form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="排序：" label-width="100">
        <el-input v-model="state.form.sort" autocomplete="off" />
      </el-form-item>
      <el-form-item label="是否启用：" label-width="100">
        <el-switch
          v-model="state.form.status"
          :active-value="1"
          :inactive-value="0"
          size="default"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="large" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button size="large" type="primary" @click="returnReasonCreateFn"
          >确 定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { Tickets } from "@element-plus/icons-vue";
import useTable from "@/hooks/useTable";
import { reactive, ref, toRefs } from "vue";
import { ElMessage, ElMessageBox, type FormInstance } from "element-plus";
import type { IReturnReasonCreateParams } from "@/types/global";
import * as returnReason from "@/request/modules/returnReason";
import type { IReturnApplyList } from "@/types/global";
import { useRouter } from "vue-router";

// 添加对话框 form表单DOM
const formRef = ref<FormInstance>();
// 添加对话框 显示隐藏
const dialogFormVisible = ref(false);
// 添加对话框 form表单数据 添加/编辑id
const state = reactive<{ form: IReturnReasonCreateParams; id: number }>({
  form: {
    createTime: "",
    name: "",
    sort: 0,
    status: 1,
  },
  id: 0,
});
const { form, id } = toRefs(state);
// 批量操作 选中
const seleOper = ref<number | string>("");
// 批量操作 列表
const seleData = reactive([
  {
    value: 1,
    label: "删除",
  },
]);
const router = useRouter();

// 调用封装组件
const {
  tableList,
  pagination,
  fetchData,
  handleSizeChange,
  handleCurrentChange,
} = useTable(returnReason.APIReturnReasonList, {
  pageNum: 1,
  pageSize: 5,
});
console.log("55555555555555555", tableList);

// 选中的列表
const selectList = ref<number[]>([]);
// 复选框按钮
const selectFn = (selection: IReturnApplyList[]) => {
  selectList.value = selection.map((item) => item.id);
};
// 全选按钮
const selectAllFn = (selection: IReturnApplyList[]) => {
  selectList.value = selection.map((item) => item.id);
};
// 添加按钮
const addFn = () => {
  id.value = 0;
  formRef.value?.resetFields();
  dialogFormVisible.value = true;
};
// 编辑按钮
const editFn = async (id2: number) => {
  id.value = id2;
  let res = await returnReason.APIReturnReasonById(id.value);
  form.value = res.data;
  dialogFormVisible.value = true;
};

// 添加/编辑 退货原因
const returnReasonCreateFn = async () => {
  if (id.value !== 0) {
    // 编辑
    let res = await returnReason.APIReturnReasonUpdateById(
      id.value,
      form.value
    );
    if (res.code === 200) {
      ElMessage.success("修改成功！");
      router.push("returnReason");
      dialogFormVisible.value = false;
      formRef.value?.resetFields();
      fetchData();
    } else {
      ElMessage.success("修改失败！");
    }
  } else {
    // 添加
    let res = await returnReason.APIReturnReasonCreate(form.value);
    if (res.code === 200) {
      ElMessage.success("添加成功！");
      dialogFormVisible.value = false;
      formRef.value?.resetFields();
      fetchData();
    } else {
      ElMessage.success("添加失败！");
    }
  }
};
// 删除按钮
const deleteFn = (id: number) => {
  ReturnReasonDeleteFn(id.toString());
};
// 批量删除按钮
const selectDeleteFn = () => {
  if (seleOper.value === 1 && selectList.value.length !== 0) {
    ReturnReasonDeleteFn(selectList.value.join(","));
  }
};
// 删除方法
const ReturnReasonDeleteFn = (ids: string) => {
  ElMessageBox.confirm("是否要进行该删除操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let res = await returnReason.APIReturnReasonDeleteById(ids);
      if (res.code === 200) {
        ElMessage.success("删除成功！");
        dialogFormVisible.value = false;
        formRef.value?.resetFields();
        fetchData();
      } else {
        ElMessage.success("删除失败！");
      }
    })
    .catch(() => {});
};
</script>

<style lang="less" scoped>
// 搜索区域
.box_card_search {
  font-size: 14px;
  margin-bottom: 20px;
  .search_header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .search_icon {
      display: flex;
      align-items: center;
      span {
        padding-left: 10px;
      }
    }
    .search_btn {
      display: flex;
      align-items: center;
      button {
        margin-left: 20px;
        padding: 9px 15px;
        font-size: 12px;
      }
    }
  }
  .el-form {
    padding: 20px 0 0;
    .el-form-item {
      width: 330px;
    }
  }
}
// 新增
.box_card_add {
  margin-bottom: 20px;
  font-size: 14px;
  .add_btn {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .add_icon {
      display: flex;
      align-items: center;
      span {
        padding-left: 10px;
      }
    }
    button {
      margin-left: 20px;
      padding: 9px 15px;
      font-size: 12px;
    }
  }
}
// 分页器
.bottom {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .el-select {
    margin-right: 20px;
  }
}
</style>
